﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;


public partial class admin_CustomerDetail : SitePage
{
    public string html = string.Empty;
    #region Fields
    public string Command
    {
        get
        {
            object o = ViewState["Command"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["Command"] = value; }

    }
   
    public int CustomerId
    {
        get
        {
            object o = ViewState["CustomerId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }
        set { ViewState["CustomerId"] = value; }

    }
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();       
    } 
    
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    private void LoadData()
    {
        if (this.Command == "edit")
        {
            busCustomer objBus = new busCustomer();
            Customer objCustomer = objBus.GetById(this.CustomerId);
            if (objCustomer != null)
            {
                if (objCustomer.IsActive)
                    chkIsActive.Checked = true;
                else chkIsActive.Checked = false;               
                txtName.Text = objCustomer.Name;
            }
        }
    }
    private void InitData()
    {
        string validateGroup = "CustomerDetail";
        vsumForm.ValidationGroup = btnSave.ValidationGroup = reqName.ValidationGroup = validateGroup;
        //Init message
        reqName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Name");
        this.Command = clsQueryString.GetString("act");
        this.CustomerId = clsQueryString.GetInt("id");     
    }  
   
    private void UpdateCustomer(Customer objData)
    {
        busCustomer objBus = new busCustomer();
        objData.CustomerId = this.CustomerId;
        int result=objBus.Update(objData);
        if (result==-1)
             lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Code");
        else if (result == 0)
            lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
        else
        {            
            Response.Redirect("CustomerList.aspx?" + this.GetPageQuery);
        }
    }

    private void InsertCustomer(Customer objCustomer)
    {
        busCustomer objBus = new busCustomer();
        int result=objBus.Insert(objCustomer);
        if (result == -1)
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Code");
        else if (result == 0)
            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
        else
        {
            
            Response.Redirect("CustomerList.aspx?" + this.GetPageQuery);
        }
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get datavalue
            Customer objData = new Customer();              
            if (chkIsActive.Checked)
                objData.IsActive = true;
            else
                objData.IsActive = false;
            objData.Name = txtName.Text;           
            if (this.Command == "edit")
                UpdateCustomer(objData);
            else
                InsertCustomer(objData);
        }
        else
        {
            vsumForm.ShowMessageBox = false;
            vsumForm.ShowSummary = true;
        }
    }    
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("CustomerList.aspx?" + this.GetPageQuery);
    }     
    #endregion   
}